using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ADF.CATIDs;
using BesAsm.Emgaats.Core;
using BesAsm.Framework.ModelResults;
using BesAsm.Framework.XpSwmmUtilities;
using BesAsm.Framework.EpaSwmm5Utilities;
using BesAsm.Emgaats.ModelResults;
using BesAsm.Framework.Reporting;

namespace BesAsm.Emgaats.ModelResultsUI
{
  [ClassInterface(ClassInterfaceType.None)]
  public class ModelResultsExtension : ESRI.ArcGIS.Desktop.AddIns.Extension, IEmgaatsExtension, IPersistVariant
  {    
    private ModelResultsUserControl _modelResultsUserControl;

    private Mediator _mediator;
    private LayerLibrarian _librarian;

    private static ModelResultsExtension _modelResultsExtension;

    public ModelResultsExtension()
    {
      _modelResultsExtension = this;      
    }

    internal static ModelResultsExtension GetExtension()
    {
      if (_modelResultsExtension == null)
      {
        UID uid = new UIDClass();
        uid.Value = ThisAddIn.IDs.ModelResultsExtension;
        ArcMap.Application.FindExtensionByCLSID(uid);
      }
      return _modelResultsExtension;
    }

    protected override void OnStartup()
    {
      base.OnStartup();

      _mediator = Mediator.AttachToMediator(this);
      _librarian = LayerLibrarian.AttachToLibrarian(this);      
      
      Mediator.ModelLoaded += ModelResultsExtension_ModelLoaded;
    }    

    #region IPersistVariant Members

    public UID ID
    {
      get
      {
        UID typeID = new UIDClass();
        typeID.Value = GetType().GUID.ToString("B");
        return typeID;
      }
    }

    public void Load(IVariantStream Stream)
    {
      //TODO: Load persisted data from document stream

      Marshal.ReleaseComObject(Stream);
    }

    public void Save(IVariantStream Stream)
    {
      //TODO: Save extension related data to document stream

      Marshal.ReleaseComObject(Stream);
    }

    #endregion   

    /// <summary>
    /// Gets the model.
    /// </summary>
    internal Model Model
    {
      get { return Mediator.Model; }
    }    

    private void ModelResultsExtension_ModelLoaded(object sender, EventArgs e)
    {
      
    }

    public bool IsExtensionCompatibleWithSystem
    {
      get
      {
        return Properties.Settings.Default.ExtensionSystemVersion ==
          Mediator.SystemVersion;
      }
    }
    
    public string Caption
    {
      get
      {
        return "Model Results Processor";
      }
    }

    public void ActivateExtension()
    {
      UID uid = new UIDClass();
      uid.Value = ThisAddIn.IDs.ModelResultsUserControl;
      IDockableWindow win = ArcMap.DockableWindowManager.GetDockableWindow(uid);
      win.Show(true);
    }

    public bool Enabled
    {
      get { return IsExtensionCompatibleWithSystem; }
    }  

  }
}